<template>
  <div>
    <!-- 顶部导航栏 -->
    <mt-header title="我的收藏" fixed>
      <span slot="left">
        <router-link to="/me">
          <mt-button icon="back"></mt-button>
        </router-link>
      </span>
    </mt-header>
    <!-- 收藏缩略图 -->
    <div class="container">
      <div
        class="collection_container"
        v-for="(item, index) of dishes"
        :key="index"
      >
        <router-link :to="`/detail/${item.id}`">
          <img class="collection_img" :src="item.image" />
          <span class="dishes">{{ item.subject }}</span>
        </router-link>

        <button class="btn_del" :id="item.id" @click="del">删除</button>
      </div>
    </div>
  </div>
</template>
<style scoped>
.mint-header {
  background-color: #e54d42;
}

.collection_img {
  width: 150px;
  height: 100px;
  border-radius: 5px;
  margin-right: 10px;
}
.collection_container {
  margin-top: 10px;
  position: relative;
}
.container {
  width: 90%;
  margin: 5%;
  margin-top: 50px;
}
.btn_del {
  width: 60px;
  height: 30px;
  background-color: #eee;
  color: #888;
  border: 0;
  border-radius: 5px;
  position: absolute;
  right: 0;
  bottom: 0;
}
.dishes {
  position: absolute;
  top: 5px;
  font-size: 23px;
  font-weight: 600;
  color: black;
}
</style>
<script>
export default {
  data() {
    return {
      dishes: [],
    };
  },
  methods: {
    del(event) {
      let menu_id = event.currentTarget.id;
      let uid = this.$store.state.user.id;
      this.axios
        .post("/del", "uid=" + uid + "&menu_id=" + menu_id)
        .then((res) => {
          console.log(res);

          this.axios
            .get("/collect_show", {
              params: {
                id: uid,
              },
            })
            .then((res) => {
              this.dishes = [];
              let results = res.data.result;
              console.log(results);
              results.forEach((item) => {
                if (item.image != null) {
                  item.image = require("../assets/articleimages/" + item.image);
                }
                this.dishes.push(item);
              });
            });
        });
    },
    // fresh() {},
  },
  mounted() {
    let uid = this.$store.state.user.id;
    this.axios
      .get("/collect_show", {
        params: {
          id: uid,
        },
      })
      .then((res) => {
        console.log(this.id);
        this.dishes = [];
        let results = res.data.result;
        console.log(results);
        results.forEach((item) => {
          if (item.image != null) {
            item.image = require("../assets/articleimages/" + item.image);
          }
          this.dishes.push(item);
        });
      });
  },
};
</script>